package org.jecat.pusher;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

public class Configure {

	public Configure(String sConfigPath) throws DocumentException
	{
		SAXReader sax = new SAXReader();
		m_aDoc = sax.read("file:///"+sConfigPath) ;
	}

	public String getString(String sPath)
	{
		String sValue = m_arrData.get(sPath) ;
		if( sValue!=null )
		{
			return sValue ;
		}
		
		Node aNode = m_aDoc.selectSingleNode(sPath) ;
		sValue = aNode==null? "": aNode.getText() ;
		
		m_arrData.put(sPath, sValue) ;
		
		return sValue ;
	}
	
	public int getInt(String sPath)
	{
		String sValue = getString(sPath) ;
		return Integer.parseInt(sValue) ;
	}

	@SuppressWarnings("unchecked")
	public ArrayList<String> getStringList(String sPath)
	{
		ArrayList<String> arrStrings = new ArrayList<String>() ;
		for( Node aNode : (List<Node>)m_aDoc.selectNodes(sPath) )
		{
			arrStrings.add(aNode.getText()) ;
		}
		
		return arrStrings ;
	}
	
	private Document m_aDoc ;
	
	private Hashtable<String,String> m_arrData = new Hashtable<String,String>() ;
	
}
